import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
// @ts-ignore
import path from "path";
import {ElementPlusResolver} from "unplugin-vue-components/resolvers";
import IconsResolver from 'unplugin-icons/resolver';
import {createSvgIconsPlugin} from 'vite-plugin-svg-icons'
import legacy from "@vitejs/plugin-legacy"

// https://vitejs.dev/config/
export default defineConfig({
  base: './',
  plugins: [vue(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver(), IconsResolver()],
    }),
    createSvgIconsPlugin({
      // 指定需要缓存的图标文件夹
      iconDirs: [path.resolve(process.cwd(), 'src/icons/svg')],
      // 指定symbolId格式
      symbolId: 'icon-[dir]-[name]',
    }),
    legacy({
      polyfills: ["es.promise.finally", "es/map", "es/set"],
      targets: ["chrome<60"],
      modernPolyfills: ["es.promise.finally"],
    }),
  ],
  server: {
    strictPort: true, // * 固定端口(如果端口被占用则中止)
    host: true, // 0.0.0.0
    port: 3820 // 指定启动端口
  },
  resolve: {
    alias: {
      "~": path.resolve(__dirname, "./"),
      "@": path.resolve(__dirname, "src"), // 路径别名
      "@@": path.resolve(__dirname, "public"),
      "static": path.resolve(__dirname, "public/static"),
      "lang": path.resolve(__dirname, "lang-data")
    },
    extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
  },
  build: {
    outDir: "output/dist",
  },
  css: {
    postcss: {
      plugins: [
        {
          postcssPlugin: 'internal:charset-removal',
          AtRule: {
            charset: (atRule) => {
              if (atRule.name === 'charset') {
                atRule.remove();
              }
            }
          }
        }
      ]
    },
    preprocessorOptions: {
      scss: {
        additionalData: `@import '@/styles/variable.scss';`
      }
    }
  }
})
